package com.one.blocks.rbac.controller.convert;

import com.one.blocks.rbac.controller.convert.worker.BaseDomainConvert;
import com.one.blocks.rbac.controller.convert.worker.TimeConvert;
import com.one.blocks.rbac.controller.query.SysRoleQuery;
import com.one.blocks.rbac.controller.vo.SysRoleVo;
import com.one.blocks.rbac.domain.SysRole;
import com.one.blocks.security.util.SubjectHelper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_CREATE_TIME;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_ID;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_BY;
import static com.one.blocks.mvc.constant.MvcConstant.CONVERT_UPDATE_TIME;


/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Mapper(uses = {TimeConvert.class}, imports = {SubjectHelper.class})
public interface SysRoleConvert extends BaseDomainConvert<SysRole, SysRoleVo, SysRoleQuery> {

    SysRoleConvert INSTANCE = Mappers.getMapper(SysRoleConvert.class);

    /**
     * 数据对象转换
     *
     * @param sysRoleVo 前端参数对象
     * @return 数据对象
     */
    @Named(value = "voToEntityIgnoreId")
    @Mapping(target = CONVERT_ID, ignore = true)
    @Mapping(target = CONVERT_CREATE_BY, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = CONVERT_CREATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = CONVERT_UPDATE_BY, ignore = true)
    @Mapping(target = CONVERT_UPDATE_TIME, ignore = true)
    @Mapping(target = "type", constant = "0")
    @Mapping(target = "state", defaultValue = "0")
    @Override
    SysRole voToEntityIgnoreId(SysRoleVo sysRoleVo);

    /**
     * 更新参数转换
     *
     * @param sysRoleVo 前端参数
     * @return 业务封装对象
     */
    @Named(value = "voToEntity")
    @Mapping(target = CONVERT_CREATE_BY, ignore = true)
    @Mapping(target = CONVERT_CREATE_TIME, ignore = true)
    @Mapping(target = CONVERT_UPDATE_BY, expression = "java(SubjectHelper.getSubjectId())")
    @Mapping(target = CONVERT_UPDATE_TIME, expression = "java(SubjectHelper.getAccessTime())")
    @Mapping(target = "type", constant = "0")
    @Override
    SysRole voToEntity(SysRoleVo sysRoleVo);
}